package cn.crdeng.dao;

import cn.crdeng.po.Blog;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface BlogRepository extends JpaRepository<Blog, Long>, JpaSpecificationExecutor<Blog> {
    //查询博客是否推荐
    @Query("select b from Blog b where b.recommend=true ")
    List<Blog> listBlogRecommend(Pageable pageable);

    //全局查询
    @Query("select b from Blog b where b.title like ?1 or b.content like ?1")
    Page<Blog> searchBlog(String query, Pageable pageable);
    //博客按年份分组
     @Query("select function('date_format',b.updateTime,'%Y')as year from Blog b group by year order by year desc ")
    List<String> getYear();

     //查询年份查询博客
     @Query("select b from Blog b where function('date_format',b.updateTime,'%Y') = ?1 ")
    List<Blog> listBlogByYear(String year);

    //添加浏览次数
    @Transactional
    @Modifying
    @Query("update Blog b set b.views=b.views+1 where b.id=?1")
    int addViews(Long id);
}
